package com.timi_licai.timi_licai_account.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


import com.timi_licai.timi_licai_account.utils.DateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 自定义View来实现画折线
 */
public class LineView extends View {
    private final static String X_KEY = "Xpos";
    private final static String Y_KEY = "Ypos";
    List<String> numList;//存放底部的日期
    List<String> weekList;//存放底部的星期
    List<Float> feeList;//存放花销的钱数
    Paint mPaint;//
    Paint pointPaint;//画点

    private List<Map<String, Integer>> mListPoint = new ArrayList<Map<String, Integer>>();

    public LineView(Context context) {
        super(context);
        init();
    }

    public LineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(5);
        pointPaint = new Paint();
        pointPaint.setColor(Color.parseColor("#AFD7DD"));
        numList = new LinkedList<>();
        weekList = new LinkedList<>();
        feeList = new LinkedList<>();

        feeList.add(0.0f);
        feeList.add(0.0f);
        feeList.add(0.0f);
        feeList.add(0.0f);
        feeList.add(0.0f);
        feeList.add(0.0f);
        feeList.add(0.0f);

    }

    public void flushData(List<Float> list) {
        feeList = new LinkedList<>();
        feeList.addAll(list);
        postInvalidate();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        //画底部的日期
        //得到控件的宽度
        float selfWidth = getWidth();//自身控件的宽度
        float selfHeight = getHeight();//自身控件的高度
        float kongxi = selfWidth / 7;//日期之间的空隙

        float dayY = selfHeight - 20;//消费为0时的Y的坐标
        List<Float> xList = new LinkedList<>();//存储各个点的X轴的坐标
        for (int i = 0; i < 7; i++) {
            xList.add(50 + i * kongxi);
        }
        //定义List存放画出各个点的坐标 根据这个list画折线
        List<Map<Float, Float>> lineXY = new LinkedList<>();
        //画出折线  有多个点和多条线组成
        //得到List中的最大的数
        float pointBaseY = dayY;//点为零时的显示的位子
        List<Float> percent = new LinkedList<>();
        float max = DateUtils.getMax(feeList);
        Log.d("^^", "最大的数是：" + max);
        for (float fee : feeList) {
            float per = ((float) fee) / ((float) (max + 5));
            percent.add(selfHeight * per);
            //   Log.d("^^", "fee:-->" + fee + "//Y 坐标：--》" + selfHeight * per);
        }

        for (int i = 0; i < feeList.size(); i++) {
            float pointX = xList.get(i);
            float pointY = pointBaseY;
            if (percent.get(i) != 0) {
                pointY = selfHeight - percent.get(i);
            }
            Map<Float, Float> pointMap = new HashMap<>();
            pointMap.put(pointX, pointY);
            lineXY.add(pointMap);
            // Log.d("^^", "画点的X坐标--》" + pointX + 25 + "画点的Y坐标--》" + pointY);
            canvas.drawCircle(pointX + 25, pointY, 10, pointPaint);
        }
        //画点之间的线
        for (int i = 0; i < lineXY.size(); i++) {
            Map<Float, Float> pointTwo = null;
            Map<Float, Float> pointOne = lineXY.get(i);
            if (i + 1 >= lineXY.size()) {
                pointTwo = lineXY.get(i);
            } else {
                pointTwo = lineXY.get(i + 1);
            }
            Set<Map.Entry<Float, Float>> entrySetOne = pointOne.entrySet();
            Set<Map.Entry<Float, Float>> entrySetTwo = pointTwo.entrySet();
            float pointOneX = 0;
            float pointOneY = 0;
            float pointTwoX;
            float pointTwoY;
            for (Map.Entry<Float, Float> entry : entrySetOne) {
                pointOneX = entry.getKey() + 25;
                pointOneY = entry.getValue();
            }
            for (Map.Entry<Float, Float> entry : entrySetTwo) {
                pointTwoX = entry.getKey() + 25;
                pointTwoY = entry.getValue();
                canvas.drawLine(pointOneX, pointOneY, pointTwoX, pointTwoY, pointPaint);
            }
        }
    }
}
